package com.ruoyi.web.controller.project;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.Project;
import com.ruoyi.system.service.IDemandService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 需求Controller
 *
 * @author EdwardXRX
 * @date 2024-04-12
 */
@RestController
@RequestMapping("/project/demand")
public class DemandController extends BaseController {
    @Autowired
    private IDemandService demandService;

    @Autowired
    private ISysUserService userService;


    /**
     * 查询需求列表
     */
    @PreAuthorize("@ss.hasPermi('project:demand:list')")
    @GetMapping("/list")
    public TableDataInfo list(Project project) {
        startPage();
        project.setType(1);
        List<Project> list = new ArrayList<>();
        Long userId = getUserId();
        if (getLoginUser().getUserId() == 1) {
            list = demandService.selectProjectList(project);
        } else {
            list = demandService.selectProjectListByUserId(userId);
        }

        return getDataTable(list);
    }

    @GetMapping("/listDemandByClickNum")
    public TableDataInfo listDemandByClickNum(Project project) {
        startPage();
        project.setType(1);
        List<Project> list = new ArrayList<>();
        list = demandService.listDemandByClickNum();
        return getDataTable(list);
    }


    /**
     * 导出需求列表
     */
    @PreAuthorize("@ss.hasPermi('project:demand:export')")
    @Log(title = "需求", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Project project) {
        List<Project> list = demandService.selectProjectList(project);
        ExcelUtil<Project> util = new ExcelUtil<Project>(Project.class);
        util.exportExcel(response, list, "需求数据");
    }

    /**
     * 获取需求详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(demandService.selectProjectById(id));
    }


    /**
     * 获取上一个需求详细信息
     */
    @GetMapping(value = "/getPreProject/{id}/{from}")
    public AjaxResult getPreProject(@PathVariable("id") Long id, @PathVariable("from") String from) {
        Long userId = getUserId();
        Project preProject = new Project();
        preProject = demandService.getPreProject(id,from,userId);
        if (preProject == null) {
            preProject = new Project();
            preProject.setId(-1L);
            preProject.setName("无");
        }
        return success(preProject);

    }

    /**
     * 获取下一个需求详细信息
     */
    @GetMapping(value = "/getNextProject/{id}/{from}")
    public AjaxResult getNextProject(@PathVariable("id") Long id,@PathVariable("from") String from) {
        Long userId = getUserId();
        Project nextProject = new Project();
        nextProject = demandService.getNextProject(id,from,userId);
        if (nextProject == null) {
            nextProject = new Project();
            nextProject.setId(-1L);
            nextProject.setName("无");
        }
        return success(nextProject);
    }


    /**
     * 新增需求
     */
    @PreAuthorize("@ss.hasPermi('project:demand:add')")
    @Log(title = "需求", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Project project) {
        project.setUserId(getUserId());
        return toAjax(demandService.insertProject(project));
    }

    /**
     * 修改需求
     */
    @PreAuthorize("@ss.hasPermi('project:demand:edit')")
    @Log(title = "需求", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Project project) {
        return toAjax(demandService.updateProject(project));
    }

    /**
     * 删除需求
     */
    @PreAuthorize("@ss.hasPermi('project:demand:remove')")
    @Log(title = "需求", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(demandService.deleteProjectByIds(ids));
    }
}
